/*
 * @Descripttion: 
 * @version: 
 * @Author: lwj
 * @Date: 2022-06-14 18:56:19
 * @LastEditors: yuanliutech@lwj
 * @LastEditTime: 2023-02-13 10:46:53
 */
const app = getApp()

import {
    validatePwd,
    validateMobile,
} from '../../../../utils/validate.js'
import {
    reqSmsCode,
    reqResetPwd,
    reqWxUserInfo
} from '../../../../request/api'
import Notify from '@vant/weapp/notify/notify';
import Toast from '@vant/weapp/toast/toast';
var WXBizDataCrypt = require('../../../../utils/WXBizDataCrypt') //解密手机号
const exitLoginBehavior = require('../../behaviors/personal/exitLogin.js')
Page({
    behaviors: [exitLoginBehavior],
    data: {
        srcEyeClose: '/statics/images/login-register/eye_close_icon.png',
        srcEyeOpen: '/statics/images/login-register/eye_open_icon.png',
        mobile: '',
        smsCode: '',
        pwd: '',
        pwd1: '',
        pwd2: '',
        isPassword: true,
        isPassword1: true,
        isPassword2: true,
        sendButtonDisabled: false, //确定按钮
    },
    // 获取手机号
    mobileChange(e) {
        this.setData({
            mobile: e.detail.value
        })
    },
    onShow() {
        console.log(app.globalData.mobile);
        this.setData({
            mobile:app.globalData.mobile
        })
    },
    // 获取本机手机号  
    getPhoneNumber(e) {
        let that = this
        wx.login({
            async success(res) {
                const res1 = await reqWxUserInfo({
                    jsCode: res.code
                }) //获取信息
                if (res1.code !== 20000) return Toast(res1.message)
                let {
                    row
                } = res1.data
                // 解密
                let appId = 'wxda69189aa728178c'
                let encryptedData = e.detail.encryptedData
                let iv = e.detail.iv
                let pc = new WXBizDataCrypt(appId, row.session_key)
                let data = pc.decryptData(encryptedData, iv)
                if (!data) return Toast("获取手机号码失败，请重试")
                let mobile = data.phoneNumber
                that.setData({
                    mobile
                })
            },
        })
    },
    // 清除手机号
    clearMobile() {
        this.setData({
            mobile: ''
        })
    },

    // 获取短信验证码
    smsCodeChange(e) {
        this.setData({
            smsCode: e.detail.value
        })
    },
    /**
     * @name: 点击获取短信
     * @msg:  mobile
     * @return {*} smsCode
     */
    async getSmsCodeClick() {
        if (this.data.showCountDown) return
        let {
            mobile
        } = this.data
        if (!validateMobile(mobile)) return
        const res = await reqSmsCode({
            mobile
        })
        if (res.code != 20000) return Toast.fail(res.data.errorInfo);
        Toast("请注意查收手机短信验证码")
        this.setData({
            showCountDown: true,
            countDown: 60,
            smsCodeMobile: mobile
        })
        let intervalId = setInterval(() => {
            let {
                countDown
            } = this.data
            if (this.data.countDown > 0 && this.data.countDown <= 60) {
                countDown -= 1
                this.setData({
                    countDown
                })
            } else {
                this.setData({
                    showCountDown: false
                })
                clearInterval(intervalId);
                intervalId = null;
            }
        }, 1000);
    },
    // 清除短信验证码
    clearSmsCode() {
        this.setData({
            smsCode: ''
        })
    },
    pwdChange(e) {
        this.setData({
            pwd: e.detail.value
        })
    },
    pwdChange1(e) {
        this.setData({
            pwd1: e.detail.value
        })
    },
    pwdChange2(e) {
        this.setData({
            pwd2: e.detail.value
        })
    },
    // pwd 密码清除
    clearPwd() {
        this.setData({
            pwd: ''
        })
    },
    // pwd1 密码清除
    clearPwd1() {
        this.setData({
            pwd1: ''
        })
    },
    // pwd2 密码清除
    clearPwd2() {
        this.setData({
            pwd2: ''
        })
    },
    // pwd 密码显隐
    showPwd() {
        this.setData({
            isPassword: !this.data.isPassword
        })
    },
    // pwd1 密码显隐
    showPwd1() {
        this.setData({
            isPassword1: !this.data.isPassword1
        })
    },
    // pwd2 密码显隐
    showPwd2() {
        this.setData({
            isPassword2: !this.data.isPassword2
        })
    },
    async resetPwdConfirm() {
        let {
            mobile,
            smsCode,
            pwd1,
            pwd2,
            sendButtonDisabled
        } = this.data
        if (sendButtonDisabled) return

        if (!validatePwd(pwd1)) return
        if (pwd1 !== pwd2) return Toast.fail("两次密码不一样")
        let params = {
            mobile,
            code: smsCode,
            passWord: pwd1,
            verifyPassWord: pwd2,
        }
        const res = await reqResetPwd(params)
        if (res.code != 20000) return Toast.fail(
            res.message
        );
        Toast.success("重置成功")
        this.setData({
            sendButtonDisabled: true
        })
        this.reqLogoutFun()
        this.clearStorage()
        setTimeout(() => {
            this.setData({
                sendButtonDisabled: false
            })
            wx.reLaunch({
                url: '/pages/login/login',
            })
        }, 1000)
    },
})